package com.chen.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.ReportMapper;
import com.chen.pojo.Report;
import com.chen.service.ReportService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 举报功能的实现
 *
 * @author chen
 * @date 2022/5/5 11:10
 */

@Service
@Transactional
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {


    @Resource
    private ReportMapper reportMapper;

    /**
     * 添加
     *
     * @param report
     * @return
     */
    public Result addReport(Report report) {
        report.setTime(new Date());
        return Result.success(reportMapper.insert(report));
    }


    public Result updateReport(Report report) {

        return Result.success(reportMapper.updateById(report));
    }


    public List<Report> getReportList() {
        Report report = new Report();
        List<Report> children = reportMapper.findChildren();
        List<Report> reportList = list();

        for (Report child : children) {
            List<Report> reportList1 = reportList.stream().filter(reports -> reports.getArticleName().equals(child.getArticleName())).collect(Collectors.toList());
            child.setChildren(reportList1);
        }


//        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return children;


    }
    public List<Report> selectReportName( String article_name){
        return  reportMapper.selectReportName(article_name);
    }


}
